<?php

namespace App\Models;

use App\Http\Library\WeChat\MiniWeChat;
use Dcat\Admin\Traits\HasDateTimeFormatter;

use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use JetBrains\PhpStorm\ArrayShape;

/**
 * App\Models\ServiceOrder
 *
 * @property int $id
 * @property string $order_id 订单编号
 * @property string $payment_no 交易单号
 * @property int $user_id 用户ID
 * @property int $shop_id 门店ID
 * @property int $worker_id 师傅ID
 * @property string $amount 支付金额
 * @property array|null $service 服务信息
 * @property string $service_id 服务ID
 * @property string|null $appoint_time 预约上门时间
 * @property int $car_id 车辆ID
 * @property array|null $car 车辆信息
 * @property int $user_coupon_id 优惠券ID
 * @property int $user_card_id 用户洗车卡ID
 * @property string $coupon_price 优惠券抵扣金额
 * @property int $province_id 省ID
 * @property int $city_id 市ID
 * @property int $district_id 区ID
 * @property int $discount 折扣
 * @property string $address 用户地址信息
 * @property string $longitude 用户位置经度
 * @property string $latitude 用户位置纬度
 * @property int $status 状态:0=待上门,1=服务中,2=已完成,3=已评价,4=已取消
 * @property int $state 状态:0=未完成,2=已完成
 * @property int $pay_way 支付方式:0=余额支付,1=微信支付,2=会员卡
 * @property int $is_apply 是否为取消申请订单 0=否,1=是
 * @property string $order_time 下单时间
 * @property string|null $receive_time 接单时间
 * @property string|null $arrive_time 上门时间
 * @property string|null $finish_time 完成时间
 * @property string|null $cancel_time 取消时间
 * @property string $refund_amount 退款金额
 * @property string|null $reason 取消原因
 * @property string|null $remark 备注信息
 * @property array|null $info1 确认时环车检查信息
 * @property array|null $info2 完成时环车检查信息
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static Builder|ServiceOrder newModelQuery()
 * @method static Builder|ServiceOrder newQuery()
 * @method static Builder|ServiceOrder query()
 * @method static Builder|ServiceOrder whereAddress($value)
 * @method static Builder|ServiceOrder whereAddressId($value)
 * @method static Builder|ServiceOrder whereAmount($value)
 * @method static Builder|ServiceOrder whereAppointTime($value)
 * @method static Builder|ServiceOrder whereArriveTime($value)
 * @method static Builder|ServiceOrder whereCouponId($value)
 * @method static Builder|ServiceOrder whereCouponPrice($value)
 * @method static Builder|ServiceOrder whereCreatedAt($value)
 * @method static Builder|ServiceOrder whereFinishTime($value)
 * @method static Builder|ServiceOrder whereId($value)
 * @method static Builder|ServiceOrder whereInfos($value)
 * @method static Builder|ServiceOrder whereOrderId($value)
 * @method static Builder|ServiceOrder whereOrderTime($value)
 * @method static Builder|ServiceOrder wherePaymentNo($value)
 * @method static Builder|ServiceOrder whereService($value)
 * @method static Builder|ServiceOrder whereServiceId($value)
 * @method static Builder|ServiceOrder whereShopId($value)
 * @method static Builder|ServiceOrder whereStatus($value)
 * @method static Builder|ServiceOrder whereUpdatedAt($value)
 * @method static Builder|ServiceOrder whereUserId($value)
 * @method static Builder|ServiceOrder whereWorkerId($value)
 * @mixin \Eloquent
 * @property \App\Models\User|null $user
 * @property \App\Models\Worker|null $worker
 * @method static Builder|ServiceOrder whereCancelTime($value)
 * @method static Builder|ServiceOrder whereCar($value)
 * @method static Builder|ServiceOrder whereCarId($value)
 * @method static Builder|ServiceOrder whereLatitude($value)
 * @method static Builder|ServiceOrder whereLongitude($value)
 * @method static Builder|ServiceOrder whereReason($value)
 * @method static Builder|ServiceOrder whereUserCouponId($value)
 * @property \App\Models\Appraise|null $apprise
 * @property \App\Models\ShopUser|null $shop
 * @method static Builder|ServiceOrder whereCityId($value)
 * @method static Builder|ServiceOrder whereDistrictId($value)
 * @method static Builder|ServiceOrder whereInfo1($value)
 * @method static Builder|ServiceOrder whereInfo2($value)
 * @method static Builder|ServiceOrder whereIsApply($value)
 * @method static Builder|ServiceOrder whereProvinceId($value)
 * @method static Builder|ServiceOrder whereReceiveTime($value)
 * @method static Builder|ServiceOrder whereRefundAmount($value)
 * @property int $is_read 是否阅读:0=否,1=是
 * @method static Builder|ServiceOrder whereIsRead($value)
 * @method static Builder|ServiceOrder wherePayWay($value)
 * @method static Builder|ServiceOrder whereState($value)
 */
class ServiceOrder extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'service_orders';

    protected $fillable = [];

    protected $casts = ['service'=>'json','info1'=>'json','info2'=>'json','car'=>'json','discount'=>'float'];

    protected static function booted(){
        self::created(function (ServiceOrder $order){
            $order_id = $order->order_id;
            $user_card_id = $order->user_card_id;
            $user_coupon_id = $order->user_coupon_id;
            if ($user_coupon_id){
                // 更新优惠券使用状态
                $data = ['status'=>1,'used_time'=>now()->format("Y-m-d H:i:s")];
                UserCoupon::whereId($user_coupon_id)->update($data);
            }
            $shop_id = $order->shop_id;
            Cache::put("isNewOrder_".$shop_id, true);
            if ($order->pay_way==0){
                $service = $order->service;
                $service_title = $service['title'];
                $user_id = $order->user_id;
                $amount = floatval($order->amount);
                $payment = PaymentOrder::whereOutTradeNo($order_id)->first();
                if ($payment){
                    $payment->pay_status = 1;
                    $payment->pay_at = now();
                    $payment->save();
                    UserMoney::money(2,$user_id,-$amount,"购买服务【{$service_title}】");
                }
            }elseif ($order->pay_way==2){
                $payment = PaymentOrder::whereOutTradeNo($order_id)->first();
                if ($payment){
                    $payment->pay_status = 1;
                    $payment->pay_at = now();
                    $payment->save();
                }
            }
            if ($shop_id){
                if ($order->worker_id){
                    self::sendMessage($order);
                }else{
                    self::sendShopMessage($order);
                }
            }
        });

        self::updating(function (ServiceOrder $order){
            $order_id = $order->order_id;
            $is_apply = $order->getOriginal("is_apply");
            $orWorkerId = $order->getOriginal("worker_id");
            $orStatus = $order->getOriginal("status");
            $dirty = $order->getDirty();
            $isFinish = $order->isDirty("status")||$order->isDirty("state");
            if($isFinish&&$order->status==2&&$order->state==1){
                OrderCommission::updateCommissionOrders($order->order_id,"洗车订单分成");
            }
            $shop_id = $order->shop_id;
            $worker_id = $order->worker_id;
            if ($order->isDirty('shop_id')&&$shop_id){
                Cache::put("isNewOrder_".$shop_id, true);
            }
            if ($order->isDirty('worker_id')&&$worker_id){
                OrderCommission::query()
                    ->where("order_id",$order_id)
                    ->where("worker_id",0)
                    ->update([
                        'worker_id'=>$worker_id
                    ]);
                if (!$orWorkerId&&$dirty['worker_id']){
                    self::sendMessage($order);
                }
                if ($orWorkerId&&$orWorkerId!=$dirty['worker_id']){
                    self::sendMessage($order);
                }
            }
            if ($order->isDirty('status')){
                if ($orStatus==1&&$order->status==2){
                    $user_id = $order->user_id;
                    $worker_id = $order->worker_id;
                    $car = $order->car;
                    $car_num = $car['plate_number'];
                    $user = User::query()->where("id",$user_id)->first();
                    $worker = Worker::query()->with(['user'])->where("id",$worker_id)->first();
                    if ($user&&$user->openid&&$worker){
                        $openid = $user->openid;
                        $workerName = $worker->name;
                        $time = now()->format("Y-m-d H:i");
                        $phone = $worker->phone;
                        self::finishSendUserMessage($openid,$car_num, $workerName, $phone,$time);
                    }
                }
                if ($order->status==4&&$order->is_apply==0&&$is_apply=1){
                    $amount = $order->amount;
                    $refund_amount = floatval($order->refund_amount);
                    if ($refund_amount>0){
                        $money = floatval($amount) - $refund_amount;
                        if ($money<0){
                            throw new Exception("退款金额不可大于订单金额!");
                        }
                    }else{
                        $money = 0;
                    }
                    $refund_no = build_order_no();
                    $payment = PaymentOrder::whereOutTradeNo($order->order_id)->first();
                    if ($payment){
                        if ($payment->pay_way==1){
                            self::refundWechat($payment, $refund_no, $refund_amount);
                        }elseif ($order->pay_way==2){
                            $discount = $order->discount;
                            if ($discount>0){
                                self::refundWechat($payment, $refund_no, $refund_amount);
                            }else{
                                self::refundCards($payment, $refund_no, $order);
                            }
                        }else{
                            self::refundPayment($payment,$refund_no);
                            $amount = $refund_amount>0?$refund_amount:$order->amount;
                            UserMoney::money(1,$order->user_id,$amount,"服务取消,退还金额");
                        }
                    }
                    $om = OrderCommission::query()->where("order_id",$order_id)->where('type',0)->first();
                    if ($om){
                        $om->amount = $money;
                        $platform_rate = floatval($om->platform_rate);
                        $worker_rate = floatval($om->worker_rate);
                        $agent_rate = floatval($om->agent_rate);
                        $top_rate = floatval($om->top_rate);
                        $top_top_rate = floatval($om->top_top_rate);
                        if (!$money){
                            $om->is_settle = 2;
                            $om->settle_time = now()->format("Y-m-d H:i:s");
                        }else{
                            $platform_money = round($money * $platform_rate/100,2);
                            $top_money = round($money * $top_rate/100,2);
                            $top_top_money = round($money * $top_top_rate/100,2);
                            $agent_money = round($money * $agent_rate/100,2);
                            $om->platform_money = $platform_money;
                            $om->top_money = $top_money;
                            $om->top_top_money = $top_top_money;
                            $om->agent_money = $agent_money;
                            $om->worker_money = $worker_rate;
                            $shop_money = $money - ($platform_money + $top_money + $top_top_money + $agent_money);
                            $om->shop_money  = $shop_money - $worker_rate;
                        }
                        $om->save();
                    }
                }
            }
        });
        self::deleting(function (ServiceOrder $order){
            OrderCommission::whereOrderId($order->order_id)->delete();
        });
    }

    public static function refundWechat(&$payment, $refund_no, $refund_amount){
        $transaction_id = $payment->transaction_id;
        $orAmount = intval(strval($payment->amount*100));
        $reAmount = intval(strval($refund_amount>0?$refund_amount*100:$orAmount));
        $wechat = MiniWeChat::instance();
        $result = $wechat->byTransactionId($transaction_id,$refund_no,$orAmount,$reAmount,[
            'refund_desc'=>'洗车订单退款'
        ]);
        if (env('APP_DEBUG')){
            Log::info($result);
        }
        if ($result['return_code']==='SUCCESS'&&$result['result_code']==='SUCCESS'){
            self::refundPayment($payment,$refund_no);
        }else{
            $msg = $result['return_msg']?:'退款失败!';
            $msg = $msg=='OK'?$result['err_code_des']:$msg;
            throw new Exception($msg);
        }
    }

    public static function refundPayment(&$payment, $refund_no){
        $payment->refund_id = $refund_no;
        $payment->refund_at = now();
        $payment->pay_status = 2;
        return $payment->save();
    }

    public static function refundCards(&$payment, $refund_no, $order){
        self::refundPayment($payment,$refund_no);
        $user_card_id = $order->user_card_id;
        $shop_id = $order->shop_id;
        $userCard = UserCard::query()->where("id",$user_card_id)->first();
        if($userCard){
            $userCard->number = $userCard->number+1;
            $userCard->save();
            $service_id = $userCard->range?$order->service_id:0;
            $user_id = $order->user_id;
            $car_id = $order->car_id;
            $where = [
                'user_card_id'=>$user_card_id,
                'service_id'=>$service_id,
                'shop_id'=>$shop_id,
                'user_id'=>$user_id,
                'car_id'=>$car_id,
            ];
            $us = UcService::query()->where($where)->first();
            if($us){
                $us->number = $us->number+1;
                $us->save();
            }
        }
    }



    public static function getStatusList(): array
    {
        return  ['待上门','服务中','已完成','已评价','已取消'];
    }

    /**
     * 添加订单
     * @param array $params
     * @return void
     */
    public static function addOrder(array $params){
        $order = new self();
        $order_id = $params['order_id'];
        $order->user_id = $params['user_id'];
        $order->order_id = $order_id;
        $order->worker_id = $params['worker_id'];
        $order->appoint_time = $params['appoint_time'];
        $order->car_id = $params['car_id'];
        $order->car = $params['car'];
        $order->service_id = $params['service_id'];
        $order->service = $params['service'];
        $order->shop_id = $params['shop_id'];
        $order->user_coupon_id = $params['user_coupon_id'];
        $order->user_card_id = $params['user_card_id'];
        $order->coupon_price = $params['coupon_price'];
        $order->longitude = $params['longitude'];
        $order->latitude = $params['latitude'];
        $order->province_id = $params['province_id'];
        $order->city_id = $params['city_id'];
        $order->district_id = $params['district_id'];
        $order->address = $params['address'];
        $order->amount = $params['amount'];
        $order->discount = $params['discount'];
        $order->pay_way = $params['pay_way'];
        $order->remark = $params['remark'];
        $order->status = 0;
        $order->order_time = date("Y-m-d H:i:s");
        if ($params['worker_id']){
            $order->receive_time = now()->format("Y-m-d H:i:s");
        }else{
            $order->receive_time = null;
        }
        $res = $order->save();
        if ($res!==false){
            if ($order->pay_way==2){
                $userCard = UserCard::query()->where("id",$params['user_card_id'])->first();
                if ($userCard){
                    $service_id = $userCard->range?$params['service_id']:0;
                    $where = [
                        'user_card_id'=>$params['user_card_id'],
                        'service_id'=>$service_id,
                        'shop_id'=>$params['shop_id'],
                        'user_id'=>$params['user_id'],
                        'car_id'=>$params['car_id'],
                    ];
                    $us = UcService::query()->where($where)->first();
                    if ($us) {
                        if($us->number){
                            $userCard->number = $userCard->number-1;
                            $userCard->save();
                            $us->number = $us->number-1;
                            $us->save();
                        }
                    }
                }
                OrderCommission::addCommission($order_id, $params, true);
            }else{
                OrderCommission::addCommission($order_id, $params);
            }
        }
    }

    protected static function model(): Builder
    {
        return self::query()
            ->with(['user'=>function($query){
                $query->select(["id","nickname","avatar"]);
            },'worker'=>function($query){
                $query->select(["id","name","phone","shop_id"]);
            },"shop"=>function($query){
                $query->select(["id","name"]);
            }])->withExists("apprise");
    }

    /**
     * 订单列表
     * @param $user_id
     * @param $worker_id
     * @return array
     */
    #[ArrayShape(['items' => "array", 'total' => "int", 'lastPage' => "int", 'options' => "array"])]
    public static function getList($user_id, $worker_id): array
    {
        $type = request()->input("type",0);
        $status = request()->input("status",0);
        $pageSize = request()->input("pageSize",10);
        $model = self::model()->when($status>-1,function ($query) use ($status, $type) {
                if ($type){
                    $query->where("status",$status);
                }else{
                    if ($status==1){
                        $query->where("state",0)->whereIn("status",[1,2]);
                    }elseif ($status==2){
                        $query->where("state",1)->whereIn("status",[2,3]);
                    }else{
                        $query->where("status",$status);
                    }
                }
            });
        if ($type){
            $longitude = request()->input('longitude',113.348717);
            $longitude = $longitude?:113.348717;
            $latitude = request()->input('latitude',23.02165);
            $latitude = $latitude?:23.02165;
            $distance = self::getDistanceField($longitude,$latitude);
            $order = $model->select("*")->addSelect(DB::raw($distance . ' as distances'))
                ->where("worker_id",$worker_id);
         }else{
            $order = $model->where("user_id",$user_id);
        }
        $list = $order->orderByDesc("id")->paginate($pageSize);
        if ($type){
            foreach ($list as $item){
                $distances = $item->distances;
                if ($distances>1000){
                    $item->distances = number_format($distances/1000,2)."km";
                }else{
                    $item->distances = number_format($distances,2)."m";
                }
            }
        }else{
            foreach ($list as &$item){
                if ($item->state==1&&$item->status==3){
                    $item->status = 2;
                }
            }
        }
        $items = $list->items();
        $total = $list->total();
        $lastPage = $list->lastPage();
        $options = [];
        if ($type){
            $statusList = [0,1,2,3,4];
            foreach ($statusList as $status){
                $options[$status] = self::model()->where("status",$status)
                    ->where("worker_id",$worker_id)->count();
            }
        }
        return [
            'items'=>$items,
            'total'=>$total,
            'lastPage'=>$lastPage,
            'options'=>$options
        ];
    }

    public static function getStatusNums($worker_id): array
    {
        $options = [];
        $statusList = [0,1,2,3,4];
        foreach ($statusList as $status){
            $options[$status] = self::model()
                ->where("status",$status)
                ->where("worker_id",$worker_id)
                ->when($status==2||$status==3,function ($query){
                    $query->where("is_read",0);
                })
                ->count();
        }
        return [
            'options'=>$options
        ];
    }

    /**
     * 计算经纬度距离
     * @param $longitude
     * @param $latitude
     * @return string
     */
    public static function getDistanceField($longitude,$latitude): string
    {
        return "(6370856*acos(cos(radians({$latitude}))*cos(radians(latitude))*cos(radians(longitude)-radians({$longitude}))+sin(radians({$latitude}))*sin(radians(latitude))))";
    }


    /**
     * 抢单列表
     * @return array
     */
    public static function getDataList($user_id): array
    {
        $pageSize = request()->input('pageSize',10);
        $longitude = request()->input('longitude',113.348717);
        $longitude = $longitude?:113.348717;
        $latitude = request()->input('latitude',23.02165);
        $latitude = $latitude?:23.02165;
        $distance = self::getDistanceField($longitude,$latitude);
        $worker = Worker::getDetail($user_id);
        $shopIds = [0];
        if ($worker){
            $shopIds = [0,$worker->shop_id];
        }
        $model = self::model()->select("*")->addSelect(DB::raw($distance . ' as distances'))
            ->whereIn("shop_id",$shopIds)->where("status",0)->where("worker_id",0);
        $list = $model->orderByDesc("id")->paginate($pageSize);
        foreach ($list as $item){
            $distances = $item->distances;
            if ($distances>1000){
                $item->distances = number_format($distances/1000,2)."km";
            }else{
                $item->distances = number_format($distances,2)."m";
            }
        }
        return [
            'items'=>$list->items(),
            'total'=>$list->total(),
            'lastPage'=>$list->lastPage(),
        ];
    }

    /**
     * 订单详情
     * @param $order_id
     * @param $worker_id
     * @return array
     */
    #[ArrayShape(['detail' => "mixed"])]
    public static function getDetail($order_id, $worker_id): array
    {
        $order = self::model()->with(['apprise','user'=>function($query){
            $query->select(["id","nickname","mobile"]);
        },'shop'=>function($query){
            $query->select(["id","name","phone"]);
        },'worker'=>function($query){
            $query->select(["id","name","phone"]);
        }])->where("order_id",$order_id)->first();
        if($order){
            if ($order->worker_id==$worker_id){
                if (($order->status==2||$order->status==3)&&$order->is_read==0){
                    $order->is_read = 1;
                    $order->save();
                }
            }
        }
        return ['detail'=>$order];
    }

    public static function refund(ServiceOrder &$serviceOrder){
        $payment_no = $serviceOrder->payment_no;
        $order = PaymentOrder::whereOutTradeNo($payment_no)->first();
        if ($order){
            if ($order->pay_status==2||$serviceOrder->status==5){
                modelError("此订单已退款");
            }
            $transactionId = $order->transaction_id;
            if ($serviceOrder->refund_amount){
                $amount = floatval($serviceOrder->refund_amount)*100;
            }else{
                $amount = floatval($serviceOrder->amount)*100;
            }
            $wechat = MiniWeChat::instance();
            $refund_id = build_order_no();
            $originAmount = floatval($order->amount)*100;
            $result = $wechat->byTransactionId($transactionId,$refund_id,$originAmount, $amount,['refund_desc'=>'洗车服务订单取消']);
            if (env('APP_DEBUG')){
                Log::info($result);
            }
            if($result['return_code']==='SUCCESS'&&$result['result_code']==='SUCCESS'){
                $serviceOrder->cancel_time = date("Y-m-d H:i:s");
                $serviceOrder->status = 4;
                $serviceOrder->save();
                $order->pay_status = 2;
                $order->refund_at = date("Y-m-d H:i:s");
                $order->save();
            }else{
                $refund_msg = $result['return_msg']=='OK'?$result['err_code_des']:'退款失败!';
                modelError($refund_msg);
            }
        }
    }

    public function user(){
        return $this->hasOne(User::class,"id","user_id");
    }

    public function worker(){
        return $this->hasOne(Worker::class,"id","worker_id");
    }

    public function shop(){
        return $this->hasOne(ShopUser::class,"id","shop_id");
    }

    public function apprise(){
        return $this->hasOne(Appraise::class,"order_id","order_id");
    }

    /**
     * 发送公众号通知 店铺负责人通知
     * @param ServiceOrder $order
     * @return void
     */
    public static function sendShopMessage(ServiceOrder $order): void
    {
        $shop_id = $order->shop_id;
        $shop = ShopUser::query()->where("id",$shop_id)->first();
        if ($shop){
            $user_id = $shop->user_id;
            $user = User::query()->where("id",$user_id)->first();
            if ($user&&$user->openid){
                $openid = $user->openid;
                $car = $order->car;
                $address = $order->address;
                $car_num = $car['plate_number'];
                $phone = $car['phone'];
                self::sendWorkerMessage($openid,$address,$car_num,$phone,['remark'=>'有新的订单,请登录后台指派洗车师傅']);
            }
        }
    }

    /**
     * 发送公众号通知 师傅通知
     * @param ServiceOrder $order
     * @return void
     */
    public static function sendMessage(ServiceOrder $order): void
    {
        $user_id = $order->user_id;
        $worker_id = $order->worker_id;
        $service = $order->service;
        $service_title = $service['title'];
        $time = $order->appoint_time?:"立即上门";
        $user = User::query()->where("id",$user_id)->first();
        if ($user&&$user->openid){
            self::sendUserMessage($user->openid,$service_title,$time, "师傅已接单");
        }
        $worker = Worker::query()->with(['user'])->where("id",$worker_id)->first();
        if ($worker&&$worker->user){
            $workerUser = $worker->user;
            $openid = $workerUser->openid;
            $car = $order->car;
            $address = $order->address;
            $car_num = $car['plate_number'];
            $phone = $user->mobile;
            self::sendWorkerMessage($openid,$address,$car_num,$phone);
        }
    }

    /**
     * 通知洗车工
     * @param $openid
     * @param $address
     * @param $car_num
     * @param $phone
     * @param array $extend
     * @return void
     */
    public static function sendWorkerMessage($openid, $address, $car_num, $phone, array $extend = []): void
    {
        $template_id = "CFM2b3CWVMVcCyN8Roi9FWIOuesPpCYpZHnq_BUc--g";
        $data = [
            "first"=>[
                "value"=>$extend['first']??"上门洗车接单通知"
            ],
            "keyword1"=>[
                "value"=>$address
            ],
            "keyword2"=>[
                "value"=>$car_num
            ],
            "keyword3"=>[
                "value"=>$phone
            ],
            "remark"=>[
                "value"=>$extend['remark']??"请及时查看"
            ],
        ];
        $wechat = MiniWeChat::instance();
        $wechat->sendMiniTempolate($openid,$template_id,$data);
    }

    /**
     * 通知用户
     * @param $openid
     * @param $service_name
     * @param $time
     * @param $status
     * @param array $extend
     * @return void
     */
    public static function sendUserMessage($openid, $service_name, $time, $status, array $extend = []): void
    {
        $template_id = "TJmCfZI7DiGyVR8n4I-q5L6BLzH4Jc7rJq_TdJeefnI";
        $data = [
            "first"=>[
                "value"=>$extend['first']??"洗车预约成功通知"
            ],
            "keyword1"=>[
                "value"=>$service_name
            ],
            "keyword2"=>[
                "value"=>$time
            ],
            "keyword3"=>[
                "value"=>$status
            ],
            "remark"=>[
                "value"=>$extend['remark']??"请及时查看"
            ],
        ];
        $wechat = MiniWeChat::instance();
        $wechat->sendMiniTempolate($openid,$template_id,$data);
    }

    /**
     * 通知用户 洗车完成
     * @param $openid
     * @param $car_num
     * @param $workerName
     * @param $time
     * @param array $extend
     * @return void
     */
    public static function finishSendUserMessage($openid, $car_num, $workerName, $phone, $time, array $extend = []): void
    {
        $template_id = "eZphi3B6icaC7nflXu-s0sGn_WFiA-cohLCx_T6sNcU";
        $data = [
            "first"=>[
                "value"=>$extend['first']??"您好，您的爱车已清洗完毕！"
            ],
            "keyword1"=>[
                "value"=>$car_num
            ],
            "keyword2"=>[
                "value"=>$workerName
            ],
            "keyword3"=>[
                "value"=>$phone
            ],
            "keyword4"=>[
                "value"=>$time
            ],
            "remark"=>[
                "value"=>$extend['remark']??"感谢您的支持，请及时查看车辆！"
            ],
        ];
        $wechat = MiniWeChat::instance();
        $wechat->sendMiniTempolate($openid,$template_id,$data);
    }


}
